import uniRequest from 'uni-request';

var baseUrl = 'http://60.204.154.40:8084/api/';//http://116.63.107.112:8089/http://192.168.1.198/https://tjyfz.jidaosec.cn:8072/'http://zhangye.zicp.net/'; // https://tjyfz.jidaosec.cn:8072/
let wsUrl = 'ws://60.204.154.40:8075';//ws://116.63.107.112:8088/ws://192.168.1.198:9326/wss://tjyfz.jidaosec.cn:8075/'
let wsHttpUrl = 'http://60.204.154.40:8074/'; // https://tjyfz.jidaosec.cn:8074
let xiaodeObsUrl = 'http://xiaode-2f00.obs.cn-east-3.myhuaweicloud.com/'
uniRequest.defaults.withCredentials=true;//让请求头携带参数cookie
uniRequest.defaults.baseURL = baseUrl;      //配置接口地址 
uniRequest.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';

uniRequest.defaults.headers.common['Token'] = uni.getStorageSync('tokenObj').token//测试
uniRequest.interceptors.request.use(
	req=>{
		// uni.showLoading({
		// 	title: '加载中',
		// 	mask: true
		// })
		
		return req
	},
	err=>{
		setTimeout(()=>{
			uni.hideLoading();
		}, 1500)
		return err
	}
)
uniRequest.interceptors.response.use(res=>{
	
	uni.hideLoading();
	let error = res.data.error||res.data.errorMsg||{};
	if(error.msg){
		uni.showToast({
			title: error.msg,
			icon: 'none',
			duration: 2000,
			mask: true
		});
	}
	if(error.code == '5001'){		
		setTimeout(()=>{
			clearInterval(getApp().globalData.msgTipsTrim);
			uni.removeStorageSync('tokenObj');
			uni.$off('login', ()=>{})
			uni.closeSocket({
				code: 1000
			});
			uni.reLaunch({
				url: '/pages/index/login'
			})
		}, 1000)
		
	}else if(error.code == '4021'){
		uni.navigateTo({
			url: '/pages/personalInfo/edit-password?type=pass&edit=1'
		})
	}
	return res
},err=>{
	// console.log(2306, err)
	uni.hideLoading()
	// if(err.response.data.state != 'ok'){
	// 	uni.showToast({
	// 		title: err.response.data.error.msg,
	// 		icon: 'none',
	// 		duration: 2000,
	// 		mask: true
	// 	})
	// }
	return err
})
function post(path, data) {
	return uniRequest.post(path, data)
}

function get(path, data) {
	return uniRequest.get(path, {
		params: data
	})
}
export default {
	post,
	get,
	baseUrl,
	wsUrl,
	wsHttpUrl,
	xiaodeObsUrl
}